package cdu.zzq.controller;
import cdu.zzq.model.User;
import cdu.zzq.service.UserService;
import cdu.zzq.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/userlist")
public class UserListServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse
            response) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();

        //
        String sPage = request.getParameter("page");
        int page = 1;
        if (sPage != null && !sPage.equals("")) {
            page = Integer.parseInt(sPage);
        }
        int pageSize = 3;
        int count = userService.count();
        int pageNum = count % pageSize ==0 ? count / pageSize : count / pageSize + 1;

        List<User> userList = userService.findByPage(page, pageSize);

        request.setAttribute("userList", userList);
        request.setAttribute("page", page);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("pageNum", pageNum);
        request.setAttribute("count", count);

        //
        RequestDispatcher dispatcher = request.getRequestDispatcher("userlist.do");
        dispatcher.forward(request, response);
    }
}